package com.example.bank.util;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author ：Leeziqiang
 * @description：TODO
 * @date ：2021/12/23 18:59
 */
@Component
public class ZipUtil {

    private static HttpServletResponse response;

    @Autowired
    private HttpServletResponse response2;

    private static HttpServletRequest request;

    @Autowired
    private HttpServletRequest request2;

    @PostConstruct
    public void beforeInit() {
        request = request2;
        response = response2;
    }

    public static void downZip(List<String> urls, String zipName) {
        // 设置响应头
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        String downloadName = zipName + ".zip";
        // 返回客户端浏览器的版本号、类型
        String agent = request.getHeader("USER-AGENT");
        try {
            // 针对IE或者以IE为内核的浏览器
            if (agent.contains("MSIE") || agent.contains("Trident")) {
                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
            } else {
                // 非IE浏览器额处理
                downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");;

        // 设置压缩流，直接写入response，实现边压缩边下载
        ZipOutputStream zipos = null;
        try {
            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            // 设置压缩方法
            zipos.setMethod(ZipOutputStream.DEFLATED);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 循环将文件写入压缩流
        DataOutputStream os = null;
        for (String url : urls) {
            try {
                URL link = new URL(url);
                String filename = url.substring(48, url.length());
                // 添加ZipEntry,并ZipEntry中写入文件流
                // 这里，加上i是防止要下载的文件有重名的导致下载失败
                zipos.putNextEntry(new ZipEntry(filename));
                os = new DataOutputStream(zipos);
                InputStream is = new BufferedInputStream(link.openStream());
                byte[] b = new byte[1204];
                int len = 0;
                while ((len = is.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                is.close();
                zipos.closeEntry();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 关闭流
        try {
            os.flush();
            os.close();
            zipos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
